package com.tk.exam.config;

import com.tk.exam.config.properties.TomcatProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;


@Slf4j
@Configuration
public class TomcatConfig {

    @Autowired
    private TomcatProperties tomcatProperties;

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(new TomcatConnectionCustomizer());
        return tomcat;
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个数据大小
        factory.setMaxFileSize(tomcatProperties.getMaxFileSize()); // KB,MB
        /// 总上传数据大小
        factory.setMaxRequestSize(tomcatProperties.getMaxRequestSize());
        return factory.createMultipartConfig();
    }


    public class TomcatConnectionCustomizer implements TomcatConnectorCustomizer {

        public TomcatConnectionCustomizer() {
        }

        @Override
        public void customize(Connector connector) {
            connector.setAttribute("connectionTimeout", tomcatProperties.getConnectionTimeout());
            connector.setAttribute("acceptorThreadCount", tomcatProperties.getAcceptorThreadCount());
            connector.setAttribute("minSpareThreads", tomcatProperties.getMinSpareThreads());
            connector.setAttribute("maxThreads", tomcatProperties.getMaxThreads());
            connector.setAttribute("maxConnections", tomcatProperties.getMaxConnections());
            connector.setAttribute("protocol", tomcatProperties.getProtocol());
            connector.setAttribute("redirectPort", tomcatProperties.getRedirectPort());
            connector.setAttribute("compression", tomcatProperties.getCompression());
            connector.setAttribute("enableLookups", tomcatProperties.getEnableLookups());
            connector.setAttribute("disableUploadTimeout", tomcatProperties.getDisableUploadTimeout());
            connector.setAttribute("compressionMinSize", tomcatProperties.getCompressionMinSize());
            connector.setAttribute("noCompressionUserAgents", tomcatProperties.getNoCompressionUserAgents());
            connector.setAttribute("compressableMimeType", tomcatProperties.getCompressableMimeType());
            //connector.setEnableLookups(Boolean.parseBoolean(tomcatProperties.getEnableLookups()));
        }
    }
}
